<template>
  <el-card>
    <el-table border style="margin: 10px 0px" :data="SkuParams">
      <el-table-column label="序号" type="index" align="center" width="100px"></el-table-column>
      <el-table-column
        label="名称"
        show-overflow-tooltip
        width="200px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        show-overflow-tooltip
        width="200px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="默认图片" width="250px">
        <!-- eslint-disable -->
        <template #="{ row, $index }">
          <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="重量(g)" width="200px" prop="weight"></el-table-column>
      <el-table-column label="价格(元)" width="200px" prop="price"></el-table-column>
      <el-table-column label="操作" width="450px" fixed="right" align="center">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            :icon="row.isSale === 1 ? 'Bottom' : 'Top'"
            @click="handleSale(row)"
          ></el-button>
          <el-button type="primary" size="small" icon="Edit"></el-button>
          <el-button
            type="primary"
            size="small"
            icon="InfoFilled"
            @click="handleInfo(row)"
          ></el-button>
          <el-popconfirm :title="`你确定要删除${row.skuName}吗？`" @confirm="handleDelete(row)">
            <template #reference>
              <el-button type="primary" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="getSku"
      @size-change="handleSizeChange"
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :background="true"
      layout="prev, pager, next, jumper,->,total, sizes"
      :total="total"
    />
    <!-- 抽屉组件 -->
    <el-drawer v-model="drawer" direction="rtl">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <div>
          <el-row style="margin: 30px 10px">
            <!-- 一行被分为24份，通过:span的值分配标题和内容的占比 -->
            <el-col :span="6">名称</el-col>
            <el-col :span="18">{{ skuInfo.skuName }}</el-col>
          </el-row>
          <el-row style="margin: 30px 10px">
            <el-col :span="6">描述</el-col>
            <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
          </el-row>
          <el-row style="margin: 30px 10px">
            <el-col :span="6">价格</el-col>
            <el-col :span="18">{{ skuInfo.price }}</el-col>
          </el-row>
          <el-row style="margin: 30px 10px">
            <el-col :span="6">平台属性</el-col>
            <el-col :span="18">
              <el-tag
                style="margin: 0px 10px 10px 10px"
                v-for="item in skuInfo.skuAttrValueList"
                :key="item.id"
              >
                {{ item.valueName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row style="margin: 30px 10px">
            <el-col :span="6">销售属性</el-col>
            <el-col :span="18">
              <el-tag
                style="margin: 0px 10px 10px 10px"
                v-for="item in skuInfo.skuSaleAttrValueList"
                :key="item.id"
              >
                {{ item.saleAttrName }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row style="margin: 30px 10px">
            <el-col :span="6">商品图片</el-col>
            <el-col :span="18">
              <!-- 轮播图 -->
              <el-carousel indicator-position="outside" height="200px">
                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                  <img :src="item.imgUrl" style="width: 100%; height: 100%" />
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #footer></template>
    </el-drawer>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
//获取相关ts数据类型
import { SkuData } from '@/api/product/sku/type'
//获取接口
import {
  reqCancelSaleSku,
  reqDeleteSku,
  reqGetSku,
  reqSaleSku,
  reqSkuInfo,
} from '@/api/product/sku/index'
//获取ElMessage组件
import { ElMessage } from 'element-plus'
//当前页面
let pageNo = ref<number>(1)
//页面显示数据数量
let pageSize = ref<number>(10)
//当前数据数
let total = ref<number>(1)
//存放获取的Sku数据
let SkuParams = ref<SkuData[]>([])
//控制抽屉页面是否显现
let drawer = ref<boolean>(false)
//储存Sku商品信息的数据
let skuInfo = ref<SkuData>()
//页面加载成功后获取Sku数据
onMounted(() => {
  getSku()
})
//获取Sku数据
const getSku = async (pager = 1) => {
  pageNo.value = pager
  let result = await reqGetSku(pageNo.value, pageSize.value)
  if (result.code === 200) {
    SkuParams.value = result.data.records
    total.value = result.data.total
  } else {
    ElMessage({
      type: 'error',
      message: '获取数据失败',
    })
  }
}
//展示数据量发生变化的回调
const handleSizeChange = () => {
  getSku()
}
//Sku上架或下架
const handleSale = async (sku: SkuData) => {
  //根据状态决定上架和下架
  //上架状态，再点一次下架
  if (sku.isSale === 1) {
    let result = await reqCancelSaleSku(sku.id)
    if (result.code === 200) {
      ElMessage({
        type: 'success',
        message: '下架成功',
      })
      sku.isSale = 0 //接口成功调用则改变对应sku的状态
    } else {
      ElMessage({
        type: 'error',
        message: '下架失败',
      })
    }
  } //下架状态，再点一次上架
  else {
    let result = await reqSaleSku(sku.id)
    if (result.code === 200) {
      ElMessage({
        type: 'success',
        message: '上架成功',
      })
      sku.isSale = 1 //接口成功调用则改变对应sku的状态
    } else {
      ElMessage({
        type: 'error',
        message: '上架失败',
      })
    }
  }
}
//显示商品信息
const handleInfo = async (sku: SkuData) => {
  let result = await reqSkuInfo(sku.id)
  if (result.code === 200) {
    skuInfo.value = result.data
    drawer.value = !drawer.value
  } else {
    ElMessage({
      type: 'error',
      message: '获取数据失败',
    })
  }
}
//删除商品信息
const handleDelete = async (sku: SkuData) => {
  let result = await reqDeleteSku(sku.id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    getSku()
  } else {
    ElMessage({
      type: 'error',
      message: '删除失败',
    })
  }
}
</script>

<style scoped lang="scss">
//轮播图的css
.el-carousel__item h3 {
  display: flex;
  color: #475669;
  opacity: 0.75;
  line-height: 300px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
